package generic.action;

import java.util.ArrayList;

import exceptions.PersistenceExceptionMessage;

import generic.dao.GenericDAO;
import generic.interfaces.GenericPersistence;
import message.erro.IdMessage;

public abstract class GenericAction<ENTITY extends GenericPersistence<?, ?, ?>, PERSISTENCE extends GenericDAO<?, ?>, TYPE_ID> {
	
	private PERSISTENCE persistence;
	
	public GenericAction(PERSISTENCE persistence) {
		setPersistence(persistence);
	}
	
	public PERSISTENCE getPersistence() {
		return persistence;
	}
	
	public void setPersistence(PERSISTENCE persistence) {
		this.persistence = persistence;
	}
	
	public boolean eNumero(String numero) {
		try {
			Integer.parseInt(numero);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<ENTITY> getEntities() throws PersistenceExceptionMessage {
		return (ArrayList<ENTITY>) getPersistence().findAll();
	}
	
	public abstract ENTITY findEntityFromForm(TYPE_ID id) throws PersistenceExceptionMessage;
	public abstract IdMessage jaExiste(ENTITY entity) throws PersistenceExceptionMessage;
	public abstract IdMessage validEntityAlter(ENTITY entity) throws PersistenceExceptionMessage;
	public abstract IdMessage validEntityPersist(ENTITY entity) throws PersistenceExceptionMessage;
	public abstract IdMessage validExclusion(ENTITY entity) throws PersistenceExceptionMessage;
	
}
